; !function (window) {
    function WsjTopNote() {
        var js = document.scripts;
        js = js[js.length - 1].src.substring(0, js[js.length - 1].src.lastIndexOf("/") + 1);

        var head = document.getElementsByTagName('head')[0];
        this.link = document.createElement('link');
        this.link.href = js + 'wsj-top-note/wsj-top-note.css';
        this.link.rel = 'stylesheet';
        this.link.type = 'text/css';
        head.appendChild(this.link);
        this.link.onload = function () {
            WsjTopNote.prototype.hasCssLoaded = true
        }
    }

    WsjTopNote.prototype = {
        __DEF__: {
            time: 3000,
            closeBtn: false,
            full: true,
            center: true,
            anim: false,
            color: 'normal'
        },
        hasCssLoaded: false,
        count: 0,
        ready: function (fn) {
            var that = this
            if (that.hasCssLoaded) {
                fn()
            } else {
                that.link.onload = function () {
                    that.hasCssLoaded = true
                    fn()
                }

            }
        },
        animationEndEvent: function () {
            var t;
            var el = document.createElement('div');
            var transitions = {
                'transition': 'animationend',
                'webkittransition': 'webkitAnimationEnd',
                'moztransition': 'mozAnimationEnd',
                'otransition': 'oAnimationEnd',
                'mstransition': 'msAnimationEnd'
            }
            for (t in transitions) {
                if (el.style[t] !== undefined) {
                    return transitions[t];
                }
            }
            return false
        },
        topNote: function (str, opts, fn) {
            var that = this
            if (that.timer) { clearTimeout(that.timer) }
            ++that.count
            if (typeof opts === 'function') {
                fn = opts
                opts = {}
            }


            if (!document.getElementById('wsj_tn_wrap')) {
                var wsj_tn_wrap = document.createElement('div');
                wsj_tn_wrap.id = 'wsj_tn_wrap'
                document.body.appendChild(wsj_tn_wrap);
            }
            var cc = "<div id='wsj_tn" + that.count + "' class='wsj-tn-body'><div id='wsj_tn_cont" + that.count +
                "' class='wsj-tn-cont'>" + str + "</div><div class='wsj-tn-close' id='wsj_tn_close" + that.count +
                "'>关闭</div></div>"
            document.getElementById('wsj_tn_wrap').innerHTML = cc
            var ele = document.getElementById('wsj_tn' + that.count);
            var note_cont = document.getElementById('wsj_tn_cont' + that.count);
            var close_btn = document.getElementById('wsj_tn_close' + that.count)
            that.ele = ele
            that.note_cont = note_cont
            that.close_btn = close_btn
            that.opts = Object.assign({}, that.__DEF__, opts)
            that.fn = fn



            var docw = document.documentElement.clientWidth || document.body.clientWidth
            var cls_btn_w
            //显示关闭按钮
            if (that.opts.closeBtn) {
                close_btn.style.display = 'table-cell'
                cls_btn_w = close_btn.offsetWidth
            } else {
                close_btn.style.display = 'none'
                cls_btn_w = 0
            }

            //全屏
            if (that.opts.full) {
                ele.style.left = 0
                ele.style.width = '100%'
                note_cont.style.maxWidth = '100%'
            } else {
                if (docw < 1025) {
                    note_cont.style.maxWidth = parseInt((docw - cls_btn_w) * 0.8) + 'px'
                } else {
                    note_cont.style.maxWidth = '600px'
                }
                ele.style.left = (docw - (note_cont.offsetWidth + cls_btn_w)) * 0.5 + 'px'
                ele.style.top = 0
            }
            //正文文字居中
            if (that.opts.center) {
                note_cont.style.textAlign = 'center'
            }

            //默认蓝色#20A0FF(normal)，绿色#13CE66(Success)，黄色#F7BA2A(Warning)，红色#FF4949(Danger)
            if (that.opts.color.indexOf('#') > -1) {
                note_cont.style.backgroundColor = opts.color
            } else {
                switch (that.opts.color.toLowerCase()) {
                    default:
                    case 'normal':
                        note_cont.style.backgroundColor = '#20A0FF' //默认蓝色#20A0FF(normal)
                        break;
                    case 'success':
                        note_cont.style.backgroundColor = '#13CE66' //绿色#13CE66(Success)
                        break;
                    case 'warning':
                        note_cont.style.backgroundColor = '#F7BA2A' //黄色#F7BA2A(Warning)
                        break;
                    case 'danger':
                        note_cont.style.backgroundColor = '#FF4949' //红色#FF4949(Danger)
                        break;
                }
            }

            if (that.animationEndEvent() && that.opts.anim) {
                that.addClass(that.ele, 'wsjTnAnimIn')
                that.ele.addEventListener(that.animationEndEvent(), that.bookTimeClose.bind(that), { capture: false, once: true })
            } else {
                that.bookTimeClose()
            }

            close_btn.addEventListener('click', function () {
                that.forceClose()
            }, false)

        },
        bookTimeClose: function () {
            var that = this
            if (that.opts.time > 0) {
                that.timer = setTimeout(function () {
                    if (typeof that.fn === "function") {
                        that.fn(that.note_cont.innerHTML, that.count)
                    }
                    if (that.animationEndEvent() && that.opts.anim) {
                        that.addClass(that.ele, 'wsjTnAnimOut')
                    } else {
                        that.ele.remove()
                    }
                }, that.opts.time)
            }
        },
        forceClose: function () {
            var that = this
            clearTimeout(that.timer)

            if (typeof that.fn == "function") {
                that.fn(that.note_cont.innerHTML, that.count)
            }

            if (that.animationEndEvent() && that.opts.anim) {
                that.addClass(that.ele, 'wsjTnAnimOut')
            } else {
                that.ele.remove()
            }
        },
        addClass: function (obj, cls) {
            //增加class
            var obj_class = obj.className,//获取class的内容；
                blank = (obj_class != '') ? ' ' : '';//判断获取的class是否为空，如果不为空，则添加空格；
            added = obj_class + blank + cls;//组合原来的class和需要添加的class，中间加上空格；
            obj.className = added;//替换原来的class；
        },
        removeClass: function (obj, cls) {
            //移除class
            var obj_class = '' + obj.className + '';//获取class的内容，并在首尾各加一个空格；'abc    bcd' -> ' abc    bcd '
            obj_class = obj_class.replace(/(\s+)/gi, ' ');//将多余的空字符替换成一个空格；' abc    bcd ' -> ' abc bcd '
            removed = obj_class.replace(' ' + cls + ' ', ' ');//在原来的class，替换掉首尾加了空格的class  ' abc bcd ' -> 'bcd '
            removed = removed.replace(/(^\s+)|(\s+$)/g, '');//去掉首尾空格；'bcd ' -> 'bcd'
            obj.className = removed;//替换原来的class；
        }
    }

    window.wsjtn = new WsjTopNote()
}(window);